/* ***** BEGIN LICENSE BLOCK *****
 * Copyright (C) 2010-2011, The VNREAL Project Team.
 * 
 * This work has been funded by the European FP7
 * Network of Excellence "Euro-NF" (grant agreement no. 216366)
 * through the Specific Joint Developments and Experiments Project
 * "Virtual Network Resource Embedding Algorithms" (VNREAL). 
 *
 * The VNREAL Project Team consists of members from:
 * - University of Wuerzburg, Germany
 * - Universitat Politecnica de Catalunya, Spain
 * - University of Passau, Germany
 * See the file AUTHORS for details and contact information.
 * 
 * This file is part of ALEVIN (ALgorithms for Embedding VIrtual Networks).
 *
 * ALEVIN is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License Version 3 or later
 * (the "GPL"), or the GNU Lesser General Public License Version 3 or later
 * (the "LGPL") as published by the Free Software Foundation.
 *
 * ALEVIN is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * or the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License and
 * GNU Lesser General Public License along with ALEVIN; see the file
 * COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * ***** END LICENSE BLOCK ***** */
package vnreal.evaluations.metrics;

import java.util.Iterator;

import org.apache.log4j.Logger;

import vnreal.demands.BandwidthDemand;
import vnreal.mapping.Mapping;
import vnreal.network.substrate.SubstrateLink;
import vnreal.resources.AbstractResource;
import vnreal.resources.BandwidthResource;

public class LinkCostPerVnr extends AbstractEvaluation {

	Logger LOG = Logger.getLogger(LinkCostPerVnr.class);
	@Override
	public double calculate() {
		return (linkCost());
	}
	
	public double linkCost(){
		String METHODNAME="linkCost() ->";
		double linkCost = 0;
		BandwidthDemand tmpBwDem;
		LOG.debug(METHODNAME+"Before for loop");
		for (Iterator<SubstrateLink> tmpSLink = stack.getSubstrate().getEdges()
				.iterator(); tmpSLink.hasNext();) {
			SubstrateLink currSLink = tmpSLink.next();
			LOG.debug(METHODNAME+"currSLink "+currSLink);
			for (AbstractResource res : currSLink) {
				if (res instanceof BandwidthResource) {
					for (Mapping f : res.getMappings()) {
						tmpBwDem = (BandwidthDemand) f.getDemand();
						LOG.debug(METHODNAME+"tmpBwDem "+tmpBwDem);
						linkCost += tmpBwDem.getDemandedBandwidth();
						LOG.debug(METHODNAME+"linkCost "+linkCost);
					}
				}
			}
		}
		RejectedNetworksNumber numRej = new RejectedNetworksNumber();
		numRej.setStack(stack);
		LOG.debug(METHODNAME+"numRej "+numRej.getValue());
		LOG.debug(METHODNAME+"stack.size() "+stack.size());
		return linkCost/((stack.size() -1) - numRej.getValue());
		
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return ("Average Link Cost");
	}
	
	

}